package com.ruoyi.factory.archive.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.factory.archive.domain.Archive;
import com.ruoyi.factory.archive.service.IArchiveService;
import com.ruoyi.factory.bom.domain.BomCost;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自定义档案Controller
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/system/archive")
public class ArchiveController extends BaseController
{
    @Autowired
    private IArchiveService archiveService;

    @PreAuthorize("@ss.hasPermi('system:archive:list')")
    @GetMapping("/getroot")
    public TableDataInfo getroot(Archive archive)
    {
        startPage();
        List<Archive> list = archiveService.selectArchiveList(archive);
        return getDataTable(list);
//        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:archive:list')")
    @GetMapping("/getchildren")
    public AjaxResult getchildren(Archive archive)
    {
        Archive archive2 = new Archive();
        Archive archive1 = archiveService.selectArchiveByPkDefdoc(archive.getPkDefdoc());
        archive2.setTypeCode(archive1.getTypeCode());
        archive2.setPcode(archive1.getCode());
        //查询出该父节点下的字节点
        List<Archive> list =  archiveService.selectArchiveList(archive2);

        return AjaxResult.success(list);

    }
    /**
     * 查询自定义档案列表
     */
    @PreAuthorize("@ss.hasPermi('system:archive:list')")
    @GetMapping("/list")
    public TableDataInfo list(Archive archive)
    {
        startPage();
        List<Archive> list = archiveService.selectArchiveList(archive);
        return getDataTable(list);
    }

    /**
     * 导出自定义档案列表
     */
    @PreAuthorize("@ss.hasPermi('system:archive:export')")
    @Log(title = "自定义档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Archive archive)
    {
        List<Archive> list = archiveService.selectArchiveList(archive);
        ExcelUtil<Archive> util = new ExcelUtil<Archive>(Archive.class);
        util.exportExcel(response, list, "自定义档案数据");
    }

    /**
     * 获取自定义档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:archive:query')")
    @GetMapping(value = "/{pkDefdoc}")
    public AjaxResult getInfo(@PathVariable("pkDefdoc") String pkDefdoc)
    {
        return AjaxResult.success(archiveService.selectArchiveByPkDefdoc(pkDefdoc));
    }

    /**
     * 新增自定义档案
     */
    @PreAuthorize("@ss.hasPermi('system:archive:add')")
    @Log(title = "自定义档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Archive archive)
    {
        return toAjax(archiveService.insertArchive(archive));
    }

    /**
     * 修改自定义档案
     */
    @PreAuthorize("@ss.hasPermi('system:archive:edit')")
    @Log(title = "自定义档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Archive archive)
    {
        return toAjax(archiveService.updateArchive(archive));
    }

    /**
     * 删除自定义档案
     */
    @PreAuthorize("@ss.hasPermi('system:archive:remove')")
    @Log(title = "自定义档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pkDefdocs}")
    public AjaxResult remove(@PathVariable String[] pkDefdocs)
    {
        return toAjax(archiveService.deleteArchiveByPkDefdocs(pkDefdocs));
    }
}
